package com.yc.auction.dao.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.yc.auction.dao.DBAccess;
import com.yc.auction.dao.IProductInfoDao;
import com.yc.auction.entity.ProductInfo;

public class ProductInfoDaoImpl implements IProductInfoDao{
	private DBAccess db=new DBAccess();
	private SqlSession session;
	

	public int add(ProductInfo productInfo) {
		int result=0;
		try {
			session=db.getSqlSession();
			result=session.insert("productIndoDao.add",productInfo);
			session.commit();
		} catch (IOException e) {
			session.rollback();
			e.printStackTrace();
		} finally{
			if(session!=null){
				session.close();
			}
		}
		return result;
	}

	public List<ProductInfo> find(Integer pageNo, Integer pageSize) {
		List<ProductInfo> list=null;
		try {
			session=db.getSqlSession();
			Map<String,Integer> map=new HashMap<String,Integer>();
			map.put("min",(pageNo-1)*pageSize);
			map.put("max",pageNo*pageSize);
			list=session.selectList("productIndoDao.find",map);
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(session!=null){
				session.close();
			}
		}
		return list;
	}

	public int update(ProductInfo productInfo) {
		int result=0;
		try {
			session=db.getSqlSession();
			result=session.update("productIndoDao.update",productInfo);
			session.commit();
		} catch (IOException e) {
			session.rollback();
			e.printStackTrace();
		} finally{
			if(session!=null){
				session.close();
			}
		}
		return result;
	}

	public List<ProductInfo> findByOnline() {
		List<ProductInfo> list=null;
		try {
			session=db.getSqlSession();
			list=session.selectList("productIndoDao.findByInfo");
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(session!=null){
				session.close();
			}
		}
		return list;
	}

}
